package com.wlc.controller;

import com.wlc.mapper.UserMapper;
import com.wlc.service.UserService;
import com.wlc.vo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王立朝
 * @date 2023/3/8
 * @description:
 */
@RestController
public class MyBatisPlusController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;
    @GetMapping("/addStudent")
    public String addStudent(){
        Student student = new Student();
        student.setName("张三");
        student.setCourse("111");
        int insert = userMapper.insert(student);
        System.out.println("insert = " + insert);
        return String.valueOf(insert);
    }

    @GetMapping("/updateStudent")
    public String updateStudent(){
        boolean flag = userService.updateStudent();
        System.out.println("flag = " + flag);
        return "更新完成";
    }

    @GetMapping("/hello")
    public String hello(){
        Student student = new Student();
        student.setName("张三");
        List<Student> users = userService.getUsers(student);
        return users.toString();
    }


    @GetMapping("/users")
    public List<Student> getUsers() {
        List<Student> students = userMapper.selectList(null);

        /*Student student = userMapper.selectById(2);
        Date updateTime = student.getUpdateTime();
        List<Student> collect = students.stream().filter(e -> e.getUpdateTime().after(updateTime)).collect(Collectors.toList());

        List<Student> collect2 = students.stream().filter(e -> e.getUpdateTime().compareTo(updateTime)>0).collect(Collectors.toList());

        List<Student> list = new ArrayList<>();
        for (int i = 0; i < students.size(); i++) {
            Student student1 = students.get(i);
            Date updateTime1 = students.get(i).getUpdateTime();
            if(updateTime1.compareTo(updateTime)>=0 && !student1.getId().equals(student.getId())){
                Student s1 = new Student();
                BeanUtils.copyProperties(student1,s1);
                list.add(s1);
            }
        }

        List<Student> collect1 = students.stream().filter(e -> e.getUpdateTime().compareTo(updateTime) == 0
                && !e.getId().equals(student.getId())).collect(Collectors.toList());
        System.out.println("collect1 = " + collect1);


        System.out.println("list = " + list);
        System.out.println("collect = " + collect);*/
        return students;

    }

    @GetMapping("/users2")
    public List<Student> getUsers2() throws ParseException {
        // 获取所有数据
        List<Student> students = userMapper.selectList(null);
        // 获取目标数据的信息
        /*Student student = userMapper.selectById(2);
        Date updateTime = student.getUpdateTime();*/

//        List<Student> collect = students.stream().filter(e -> e.getUpdateTime().compareTo(updateTime) >= 0).collect(Collectors.toList());
//        System.out.println("collect = " + collect);


        // 大于 目标的更新时间
//        List<Student> collect1 = students.stream().filter(e -> e.getUpdateTime().compareTo(updateTime) > 0).collect(Collectors.toList());
//        System.out.println("collect1 = " + collect1);

        /*Date date = new Date();
        List<Student> collect = students.stream().filter(e -> date.compareTo(e.getUpdateTime()) < 0).collect(Collectors.toList());
        System.out.println("collect = " + collect);*/
        String sStr = "2023-04-16 09:59:56";
        String eStr = "2023-04-16 10:39:12";
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = sf.parse(sStr);
        Date endTime = sf.parse(eStr);
        // 未发生的会议

        List<Student> collect = students.stream().filter(e -> e.getCourse().equals("9") && endTime.compareTo(e.getStartTime()) < 0 && startTime.compareTo(e.getStartTime())<0).collect(Collectors.toList());
        System.out.println("collect = " + collect);

        return students;

    }
}
